#include "tcpclient.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"

tcp_client::tcp_client(char* server_ip,char* server_port)
{
       if( (socket_fd = socket(AF_INET,SOCK_STREAM,0)) < 0 ) {
                printf("create socket error: %s(errno:%d)\n)",strerror(errno),errno);
                exit(0);
        }

        memset(&server_addr,0,sizeof(server_addr));
        server_addr.sin_family = AF_INET;
        server_addr.sin_port = htons(atoi(server_port));

        if( inet_pton(AF_INET,server_ip,&server_addr.sin_addr) <=0 ) {
                printf("inet_pton error for %s\n",server_ip);
        }

        if( connect(socket_fd,(struct sockaddr*)&server_addr,sizeof(server_addr))<0) {
                printf("connect error: %s(errno: %d)\n",strerror(errno),errno);
        }

        printf("send message to server: \n");
        char buff[]="{\"count\":10,\"data\":[{\"fileKey\":\"68c573f39e2f00fbf6e06abd4b045693\"},{\"fileKey\":\"bdf958350af6fb576c5e25b5659c4a65\",\"fileType\":\"C\"},{\"fileKey\":\"9c69bc7169730f016b2a8b53047743c2\"},{\"fileKey\":\"3ef889ca546f9b8d5bd4691d5c7f6332\",\"fileType\":\"D\"},{\"fileKey\":\"da616e7af459029d59873235f57e4071\"},{\"fileKey\":\"476d98b4566a2f14465e2b6d413b414e\"},{\"fileKey\":\"09d3187ead5bca8fb2db5ff5a269c580\"},{\"fileKey\":\"2adc93ce76223a6b6cdb386ba7ca55b3\"},{\"fileKey\":\"6e8dbbf94086e50cc1d7b774b3055793\"},{\"fileKey\":\"49287ae5acf0cb26692f94eaf40d03f1\"}]}";
		if(int res= send( socket_fd,buff,strlen(buff),0 ) < 0 ) {
                printf("send message error %d\n",res);
        }else 
			printf("send message to server: \n");
		close(socket_fd);

}

void *client_fun(void *arg)
{
	tcp_client tc("172.16.10.41","20000");  
}

int main(int argc,char* argv[])  
{ 
	//tcp_client tc("172.16.10.41","20000");
	for(int i = 0;;i++)
	{
		pthread_t thread_id; 
		pthread_create(&thread_id,NULL,client_fun,NULL);  //创建线程 
		pthread_detach(thread_id); // 线程分离，结束时自动回收资源
		if(i % 5 == 0)
			usleep(2000); 
	}
	return 0;  
}  

